<template>
  <el-dialog
    :visible.sync="visible"
    :before-close="beforeClose"
    :close-on-click-modal="false"
    title="还款管理"
    append-to-body
    class="dialog-sm"
    top="30px"
    width="1000px"
  >
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          @keyup.enter.native="getData()"
          placeholder="项目名称查询"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          @keyup.enter.native="getData()"
          placeholder="负责人查询"
          :clearable="true"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          :auto-top="true"
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <repay-dialog :visible.sync="replayVisible" :query-id="queryId"></repay-dialog>
  </el-dialog>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import { mapGetters } from 'vuex'
import RepayDialog from '@/credit/views/city/self/after/repay/RepayDialog'
export default {
  components: {
    SelectAddress,
    RepayDialog,
  },
  props: {
    visible: Boolean,
  },
  data() {
    return {
      order: {
        N1is_history: 'asc',
        N2create_time: 'desc',
      },
      statusOptions: {
        0: '待确认',
        1: '正常',
        2: '异常',
      },
      params: {
        index: 1,
        size: 10,
        'loan_approval_status.eq': 3,
        'confuse_status.neq': 3,
      },
      exportLoading: false,
      replayVisible: false,
      loading: false,
      tableData: [],
      tableColumns: [
        {
          attrs: { label: '主体名称', prop: 'customer_name', align: 'center' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '地址', prop: 'address', align: 'center' } },
        { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        {
          attrs: { label: '到期日期', prop: 'load_end_date', align: 'center' },
          process: v => this.$utils.dateFormat(v, 'yyyy-MM-dd'),
        },
        { attrs: { label: '放款金额', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '在保余额', prop: 'left_funds', align: 'center' } },

        {
          attrs: { label: '操作', align: 'center' },
          operations: [{ content: '更新还款信息', icon: 'fa fa-edit', event: this.handleEdit }],
        },
      ],
      totalCount: 0,
      tableName: 'customer_related',
      buttonLoading: false,

      queryId: '',
    }
  },
  computed: {
    ...mapGetters(['userId', 'clientId']),
    clientId() {
      return this.$store.getters.clientId
    },
  },
  watch: {},
  mounted() {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getData()
  },
  methods: {
    handleEdit(row = {}) {
      this.queryId = row.customer_related_id || ''
      this.replayVisible = true
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
    fetchData() {
      return this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params, this.order || {})
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData()
      this.loading = false

      const data = res.data || {}
      this.totalCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    dataAdapter(item) {
      return item
    },
  },
}
</script>
<style lang="scss" scoped></style>
